package com.fjec.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.market.entity.BizFollow;
import com.fjec.market.service.IBizFollowService;
import com.fjec.market.service.IInfoMarketService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 017.关注店铺表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-16
 */
@RestController
@RequestMapping("/")
public class BizFollowController {

    @Resource
    private IBizFollowService service;

    @Resource
    private IInfoMarketService infoMarketService;

    @PostMapping("/y/follow/save")
    public JsonResult saveFollow(BizFollow bizFollow) {
        bizFollow.setCreateTime(DateUtil.nowLdt());
        service.save(bizFollow);
        return JsonResult.success().message("关注成功").build();
    }

    /**
     * 用户秒杀和收取礼物后自动关注赠与方店铺
     * @param userId
     * @param busiId
     * @param busiType
     * @return
     */
    @PostMapping("/follow/seckillAutoFollow")
    public Integer seckillAutoFollow(Long userId, Long busiId, Integer busiType) {
        int exist = service.count(new QueryWrapper<BizFollow>()
                .eq("UserId", userId)
                .eq("BusiId", busiId)
                .eq("BusiType", busiType));
        if (exist > 0)
            return 0;
        BizFollow bizFollow = new BizFollow();
        bizFollow.setUserId(userId);
        bizFollow.setBusiId(busiId);
        bizFollow.setBusiType(busiType);
        bizFollow.setCreateTime(DateUtil.nowLdt());
        boolean saved = service.save(bizFollow);
        if (saved)
            return 0;
        else
            return -1;
    }

    /**
     * 取消关注
     * @param
     * @return
     */
    @PostMapping("/y/follow/delete")
    public JsonResult deleteFollow(Long userId, Long busiId, Integer busiType) {
        service.remove(new QueryWrapper<BizFollow>().eq("UserId", userId).eq("BusiId", busiId).eq("BusiType", busiType));
        return JsonResult.success().message("取消关注成功").build();
    }

    @GetMapping("/y/follow/getMyFollowing")
    public JsonResult getMyFollowing(Long userId, Integer busiType, Page page) {
        return JsonResult.success().data(infoMarketService.getMyFollowing(userId, busiType, page)).build();
    }
}
